package leetcode_2501_2600;

public class LeeCode_2541 {
    public static void main(String[] args) {
        System.out.println(minOperations(new int[]{10, 5, 15, 20}, new int[]{20, 10, 15, 5}, 0));
    }
    private static long minOperations(int[] nums1, int[] nums2, int k) {
        int n = nums1.length;
        long c1 = 0, c2 = 0;
        if (k == 0){
            for (int i = 0; i < n; i++) {
                if (nums1[i] != nums2[i])
                    return -1;
            }
            return 0;
        }
        for (int i = 0; i < n; i++) {
            int diff = nums1[i] - nums2[i];
            int t = diff < 0 ? -1 : 1;
            diff = diff < 0 ? -diff : diff;
            if (diff % k == 0){
                int res = diff / k;
                if (t == -1){
                    c1 += res;
                }else {
                    c2 += res;
                }
            }else {
                return -1;
            }
        }
        if (c1 != c2)
            return -1;
        return c1;
    }
}
